package io.renren.modules.substation.service.impl;

import io.renren.modules.substation.entity.TInfoAreaInfoEntity;
import io.renren.modules.sys.entity.SysRoleMenuEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.substation.dao.TInfoUserAreaDao;
import io.renren.modules.substation.entity.TInfoUserAreaEntity;
import io.renren.modules.substation.service.TInfoUserAreaService;
import org.springframework.transaction.annotation.Transactional;


@Service("tInfoUserAreaService")
public class TInfoUserAreaServiceImpl extends ServiceImpl<TInfoUserAreaDao, TInfoUserAreaEntity> implements TInfoUserAreaService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TInfoUserAreaEntity> page = this.page(
                new Query<TInfoUserAreaEntity>().getPage(params),
                new QueryWrapper<TInfoUserAreaEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveUserArea(Long userId, List<Long> userAreaList, Long createId) {
        //先删除用户与区域关系
        deleteBatch(new Long[]{userId});

        if(userAreaList.size() == 0){
            return ;
        }

        //保存用户与区域关系
        for(Long areaId : userAreaList){
            TInfoUserAreaEntity tInfoUserAreaEntity = new TInfoUserAreaEntity();
            if (areaId < 0L){//设备ID
                tInfoUserAreaEntity.setUserArea(0L - areaId);
                tInfoUserAreaEntity.setAreaType("1");
            } else {//区域
                tInfoUserAreaEntity.setUserArea(areaId);
                tInfoUserAreaEntity.setAreaType("0");
            }
            tInfoUserAreaEntity.setUserId(userId);
            tInfoUserAreaEntity.setCreateOpr(createId);
            tInfoUserAreaEntity.setCreateTime(new Date());
            this.save(tInfoUserAreaEntity);
        }
    }

    @Override
    public int deleteBatch(Long[] userIds){
        return baseMapper.deleteBatch(userIds);
    }

    @Override
    public List<TInfoUserAreaEntity> listByUserId(Long id) {
        return baseMapper.listByUserId(id);
    }


}